package cn.hxt.org.service.impl;

import cn.hxt.basic.exception.BusinessException;
import cn.hxt.basic.query.BaseQuery;
import cn.hxt.basic.service.impl.BaseServiceImpl;
import cn.hxt.basic.util.PageList;
import cn.hxt.org.domain.Employee;
import cn.hxt.org.domain.Shop;
import cn.hxt.org.mapper.EmployeeMapper;
import cn.hxt.org.mapper.ShopMapper;
import cn.hxt.org.service.IShopService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void settlement(Shop shop) {
        //校验，
        //1，先获取Admin，判断两次密码是否一致，不一致，抛异常
        Employee admin = shop.getAdmin();
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("密码不一致！！！！！！！");
        }
        //2,校验账户是否被注册，就是判断employee表中的username
        Employee userName = employeeMapper.findByUserName(admin.getUsername());
        //再判断username为不为空，不为空说明用户名存在，存在就抛异常
        if(userName != null){
            throw  new BusinessException("用户名已存在！！！");
        }
        //以上都没问题我们就注册
        //1，先添加employee表，返回自增id，employee_id
        employeeMapper.save(admin);
        //2，再添加shop店铺admin_id，返回自增id,shop_id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        //将shop中的admin_id赋值给admin，
        admin.setShop_id(shop.getId());
        //再由save进行修改
        employeeMapper.update(admin);
    }
}
